package com.yoomiito.app.ui.songduoduo

import android.text.TextUtils
import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.base.AppConfig
import com.yoomiito.app.base.BaseLazyFragmentPresenter
import com.yoomiito.app.model.BaseList
import com.yoomiito.app.model.ViewType
import com.yoomiito.app.model.sdd.*
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.utils.YMToast

class SendMoreFragmentPresenter(appConfig: AppConfig) : BaseLazyFragmentPresenter<SendMoreFragment>(appConfig){

    fun freeGoodsList(){
        YmApi.getYmService()
                .sddFreeGoodsList()
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<SddFreeGoodsList>(){
                    override fun onSuccess(t: SddFreeGoodsList?) {
                        v.freeGoodsData(t)
                    }

                    override fun onFail(error: ApiException?) {

                    }

                })
    }

    fun vipGoodsList(page:Int){
        YmApi.getYmService()
                .sddVipGoodsList(page,20)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<BaseList<SddVipGoodsInfo>>(){
                    override fun onSuccess(t: BaseList<SddVipGoodsInfo>?) {
                        val data = BaseList<ViewType<SddVipGoodsInfo>>()
                        data.data = mutableListOf<ViewType<SddVipGoodsInfo>>()
                        data.current_page = t?.current_page!!
                        data.last_page = t?.last_page
                        t?.data?.forEachIndexed { index, sddVipGoodsInfo ->
                            //0  展示大图   1 展示小图
                            data.data.add(ViewType(if (t?.current_page==1&&index<2) SendMoreGoodsAdapter.BigImageType else SendMoreGoodsAdapter.SmallImageType,sddVipGoodsInfo))
                        }
                        v.vipGoodsData(data)
                    }

                    override fun onFail(error: ApiException?) {

                    }

                })
    }

    /**
     * 送多多--用户数据
     */
    fun sddUserInfo(){
        YmApi.getYmService()
                .sendMoreInfo()
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<SddUserData>(){
                    override fun onSuccess(t: SddUserData?) {
                        v.vipUserData(t)
                    }

                    override fun onFail(error: ApiException?) {

                    }

                })
    }

    /**
     * 送多多--获取分享信息
     * @param type
     * @param goodsId
     */
    fun getSddShareMsg(type: Int) {
        YmApi.getYmService()
                .shareMsg(type, "")
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<ShareMsg?>() {
                    override fun onSuccess(shareMsg: ShareMsg?) {
                        v.sddShareMsg(shareMsg)
                    }

                    override fun onFail(error: ApiException) {
                        v.dismissLoading()
                        if (!TextUtils.isEmpty(error.msg)) YMToast.showCenterToast(error.msg)
                    }
                })
    }

}